$(function(){
    var currentCity="";
    var l_s;
    if(window.localStorage){
        l_s=window.localStorage;      
    }else{
        l_s={};
    }
    var htmlCity={
        city:'',
        getCity:function (){

            console.log(111+"--"+localStorage.currentCity);
            if(window.localStorage&&window.localStorage.currentCity!=null){
                city=localStorage.currentCity;
                localStorage.city=null;
                if($(".search-left span")){
                    $("#address").html(city);
                }
            }else{
                var c=SYS_GLOBAL_CONFIG.getCurrentCity("currentCity");
                var c_City;
                if(c.indexOf("北京市")>-1){
                    c_City="北京";
                }else{
                    c_City=c;
                }
                if(c_City!=undefined&&c_City!=""){
                     $("#address").html(c_City);        
                }else{
 htmlCity.getAddress(htmlCity.reqUrl,htmlCity.addr_success);    
                }
            }
        },
        reqUrl:'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',
        addr_success:function (_result){
 
            if (remote_ip_info.ret == '1') {
                console.log("数据"+remote_ip_info);
                if(remote_ip_info.city.indexOf("北京市")>-1){
                    htmlCity.city="北京";    
                }else{
                    htmlCity.city=remote_ip_info.city;//获取到当前城市
                }
                htmlCity.setCity(htmlCity.city);//缓存当前的城市
            } else {
                htmlCity.city="深圳市";
            }
        },
        /**
         *将当前城市缓存到localStorage中
         **/
        setCity:function(city_name){
            if(window.localStorage){
                htmlCity.city=localStorage.currentCity||city_name;
                localStorage.currentCity=htmlCity.city;
                if($(".search-left span")){
                    $(".search-left span").html(htmlCity.city);
                }  
            }else{
                console.log("对不起，您的浏览器不支持localstorage"); 
            }  
        },
        getAddress:function(reqUrl,callback){
            $.getScript(reqUrl,callback);
        }
    }
    /**
     *获取当且定位城市
     **/
    htmlCity.getCity();
    var url=SYS_GLOBAL_CONFIG.reqUrl,
    asyn="true",
    type="post";
    if($("#address").html().indexOf("定位中")>-1){
        currentCity="深圳市";
    }else{
        currentCity=$("#address").html();        
    }
    var address="";
    if(currentCity.indexOf("北京市")>-1){
        address="北京";
    }else{
        address=currentCity;
    }
     /**
     *点击加载更多,跳转进入到不同的页面
     **/
    $(".more").on("click",function(){
        var urlName=$(this).attr("data-url");
        url="./"+urlName+".html?currentCity="+currentCity;
        console.log(url);
        location.href=url;
    });
    console.log(address+"--------------"+currentCity);
    var dataProduct=JSON.stringify({//获取产品
        "code":"getProductList",
        "version":"1.0",
        "data":{
            "currentPage":1,
            "address":address,
            "productType":"",
            "showCount":6
        }
    });
    
    console.log("当前城市："+address);
    var dataShop=JSON.stringify({
        "code":"getShopList",
        "verson":"1.0",
        "data":{
            "salesManId":"",
            "address":address,
            "sellerType":"",
            "resType":"1",
            "currentPage":1,
            "showCount":6
        }
    });
    console.log(dataShop);
    var dataService=JSON.stringify({
        "code":"getLocalService",
        "version":"1.0",
        "data":{
            "productType":"SERVICE",
            "address":address,
            "serviceId":"",
            "resType":"1",
            "currentPage":1,
            "showCount":6
        }
    });
    //点击搜索，跳转到搜索页面
    $("#search-input").on("click",function(){
        l_s.search_state=1;
        location.href="./searchGoods.html?currentCity="+address;
    });

    function getData(url,requestData,call_func,asyn,type){
        $.ajax({
            url:url,
            type:type,
            datatype:"json",
            contentType:"application/json",
            data:requestData,
            async:asyn,
            success:function(data){
                call_func(data);
            },
            error:function(msg){
                console.log("异常："+msg);
            }   
        });        
    }
    /**
     *商品信息
     **/
    function call_func1(data){
        console.log(data);
        var datas=data.data.datas;
        var lens=datas.length;
        totalPage=data.data.totalPage;//总页数
        var dataStr1="";
        var dataStr2="";
        var arr1=[];
        var arr2=[];
        if(lens>0){
           for(var i=0;i<lens;i++){
                if(datas[i].sellerFees.length>0){
                    if(datas[i].sellerFees[0].isAvaliable==1){
                        arr2.push(datas[i]);    
                    }else{
                        arr1.push(datas[i]);
                    }
                }else{
                    arr1.push(datas[i]);
                }
           }
        }
        if(arr1.length>0){
            for(var i=0,len=arr1.length;i<len;i++){
               var isauth="";
               if(arr1[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
               dataStr1+='<div data-proId="'+arr1[i].productId+'" class="zwl-noCertificate"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr1[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr1[i].productName+'</p><span class="shop1">'+arr1[i].productTypeName+'</span><span class="shopRe1">'+isauth+'</span><span class="span2"> <span class="money">'+arr1[i].saleprice+'元/</span><span class="cess">'+arr1[i].unit+'</span></span></div></div></div>';

           }
       }
        var downS='';
        if(arr2.length>0){
            for(var i=0,len=arr2.length;i<len;i++){
               var isauth="";
               if(arr2[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
                var main_img='';
                var k_arr=arr2[i].productImgs;
                var k_len=k_arr.length;
                if(k_len>0){
                    for(var k=0;k<k_len;k++){
                        if(k===3){
                            break;    
                        }
                        main_img+='<img src="'+k_arr[k].FULLPATH+'" alt="">';    
                    }    
                }
                var t_len=arr2[i].tagServices.length;
                var arrTag=arr2[i].tagServices;
                if(t_len>2){
                    downS='<i class="down"></i>';
                }
                var tags='';
                var tagStr='';
                for(var t=0;t<t_len;t++){
                    tagStr+='<p class="tags_info"><img src="'+arrTag[t].tagIconPath+'" alt=""><span>'+arrTag[t].userRemark+'</span></p>';
                    if(t<3){
                        tags+='<span>'+arrTag[t].name+'</span>'; 
                    }
                }
                dataStr2+='<div class="zwl-Certificate"><div class="zwl-show" data-proId="'+arr2[i].productId+'"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr2[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr2[i].productName+'</p><p class="zwl-way">'+tags+'</p><span class="shop">'+arr2[i].productTypeName+'</span><span class="shopRe">'+isauth+'</span><span class="span1"><span class="money">'+arr2[i].saleprice+'元/</span><span class="cess">'+arr2[i].unit+'</span></span></div></div><div class="zwl-pic">'+main_img+'<img src="'+arr2[i].mainImgPath+'" alt=""></div></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
           }
       }
        $("#goods-lists").append(dataStr1+""+dataStr2);
        var down_flag=1;
        if(downS!=''){
            $(".zwl-benefit i").off("click");
            $(".zwl-benefit i").on("click",function(){
                if(down_flag==1){  
                    $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                    down_flag=0;
                }else{
                    $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                    down_flag=1;
                }
            });    
        }
        $(".zwl-noCertificate,.zwl-show").on("click",function(){
            var proId=$(this).attr("data-proId");
            location.href="./goodDetail.html?productId="+proId;
        });
   }
    /**
     *服务信息列
     **/
    function call_func2(data){
        console.log(data);
        var datas2=data.data.datas;
        var len2=data.data.datas.length;
        var li="";
        for(var i=0;i<len2;i++){
            var pic="";
            var fulladdress=datas2[i].address;
            var addr=fulladdress.split(" ");
            var imgsLen=datas2[i].serviceImgs.length;
            if(imgsLen>0){
                for(var j=0;j<imgsLen;j++){
                    if(j===3){
                        break;
                    }
                    pic+='<img class="l"  src="'+datas2[i].serviceImgs[j].FULLPATH+'" alt="">'
                 }
            }else{
                pic="";
            }

            if(datas2[i].userAuthStatus==1){
                img1='<img class="img-v" src="./images/vip_icon.png" alt="">';

            }else{
                img1=""; 
            }
            li+='<div class="s-detail" data-productId="'+datas2[i].productId+'">'+
                    '<aside class="l-aside l">'+
                        '<img src="'+datas2[i].userImgPath+'" alt="">'+
                    '</aside>'+
                    '<aside class="r-aside l">'+
                        '<div class="raside-name clear">'+
                            '<div class="pro l">'+
                                '<p class="pro-name">'+datas2[i].productName+'</p>'+
                                 '<i>'+datas2[i].saleprice+'/'+datas2[i].unit+'</i>'+
                                '<p class="user-name"><span class="small-light">'+datas2[i].userName+'</span>'+img1+'<span class="zwl-as">'+addr[2]+'/'+addr[3]+'</span></p>'+
                            '</div>'+

                            '<div class="clears"></div>'+
                        '</div>'+
                        '<p class="raside-explain">'+datas2[i].remark+'</p>'+
                        '<div class="raside-img">'+pic+'<div class="clears"></div>'+
                        '</div>'+
                        '<div class="raside-detail ">'+
                               '<span class="detail-info">'+datas2[i].parentServiceName+'</span>'+
                               '<img class="mag-l20" src="./images/shoucang.png" alt=""><span class="detail-info">'+datas2[i].serviceCollectCount+'</span>'+
                               '<img class="mag-l20"src="./images/liulan.png" alt=""><span class="detail-info">'+datas2[i].viewCount+'</span>'+
                        '</div>'+
                    '</aside>'+
                    '<div class="clears"></div>'+
                '</div>';
               
        }
        $(".serviceDetail").append(li);  
        $(".currentpage").html(data.data.currentPage);
        $(".s-detail").on("click",function(){
            var productId=$(this).attr("data-productId");
            url="./serviceDetail.html?productId="+productId;
            location.href=url;
        });     
    }

    function call_func3(data){
        var dataS=data.data.datas;
            var s_len=dataS.length;
            var img="";
            var registr="";
            var attr="";
            totalPage=data.data.totalPage;
            var li="";
            var downS='';
            for(var i=0;i<s_len;i++){
                if(dataS[i].seller){
                    if(dataS[i].seller.isSellerAuth=="1"){
                        img='<img class="img3" src="./images/vip_icon.png" alt="">';
                        registr="(已认证)";
                    }else{
                        img="";
                        registr="(未认证)";
                    }    
                }
                var tagStr='';
                var tagS='';
                if(dataS[i].sellerFees){
                    if(dataS[i].sellerFees.length>0){
                        console.log(dataS[i].sellerFees.length);
                        if(dataS[i].sellerFees.length>0){
                            var SellerFree=dataS[i].sellerFees[0].isAvaliable;
                            if(SellerFree=="1"){//已开通增值服务
                                var s_tag=dataS[i].tagServices;
                                var ss_len=s_tag.length;
                                if(ss_len>2){
                                    downS='<i class="down"></i>';
                                }
                                for(var s=0;s<ss_len;s++){
                                    tagStr+='<p class="tags_info"><img src="'+s_tag[s].tagIconPath+'" alt=""><span>'+s_tag[s].userRemark+'</span></p>';
                                    if(s<3){
                                        tagS+='<span>'+s_tag[s].name+'</span>';
                                    }
                                }        
                            }       
                        }
                    }    
                }

               li+='<div class="detail-shop" data-shopId="'+dataS[i].shopId+'">'+
                    ' <img class="img2" src="'+dataS[i].logoImgPath+'" alt="">'+
                    '<div class="introduce">'+
                    ' <div class="introduce-detail">'+
                    '<div class="introduce-name">'+
                    '<span class="span-detail"><span class="span-name">'+dataS[i].shopName+'</span>'+ 
                    ''+img+'</span><p class="span-tags">'+tagS+'</p>'+
                    ' <span class="cess r1">'+dataS[i].shopTypeName+'</span><span class="regstr r1">'+registr+'</span> </div>'+
                    '<div class="clears"></div></div></div><div class="clears"></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
            }
            $("#detail").append(li);
            var down_flag=1;
            if(downS!=''){
                $(".zwl-benefit i").off("click");
                $(".zwl-benefit i").on("click",function(){
                    if(down_flag==1){  
                        $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                        down_flag=0;
                    }else{
                        $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                        down_flag=1;
                    }
                });    
            }
            $(".detail-shop .img2,.detail-shop .introduce").on("click",function(){
                var shopId =$(this).parent(".detail-shop").attr("data-shopId");
                url = "shopDetail.html?shopId="+shopId;//此处拼接内容
                location.href = url;
            });
        } 
    //获取附近商品信息
    getData(url,dataProduct,call_func1,asyn,type);
    //获取附近服务信息
    getData(url,dataService,call_func2,asyn,type);
    //获取附近商家信息
    getData(url,dataShop,call_func3,asyn,type);
    
    $(".zwl-good").on("click",function(){
        location.href="./goods.html?currentCity="+currentCity;
    });
    $(".zwl-shop ").on("click",function(){
        location.href="./shop.html?currentCity="+currentCity;
    });
    $(".zwl-service").on("click",function(){
        location.href="./service.html?currentCity="+currentCity;
    });
    
});
